package com.yunhe.transfer.service.data.impl;

import com.yunhe.common.model.authority.Station;
import com.yunhe.transfer.client.AuthorityClient;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.authority.StationDTO;
import com.yunhe.common.model.model.RunStrategyDistributeDTO;
import com.yunhe.transfer.mqtt.RunStrategyMqttHandler;
import com.yunhe.transfer.service.data.RunStrategyService;
import com.yunhe.transfer.websocket.RunStrategyWebsocketHandler;
import com.yunhe.common.util.CollectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RunStrategyServiceImpl implements RunStrategyService {

    @Autowired
    private RunStrategyWebsocketHandler runStrategyWebsocketHandler;
    @Autowired
    private RunStrategyMqttHandler runStrategyMqttHandler;
//    @Autowired
//    @Qualifier("allStations")
//    private List<StationDTO> stations;
    @Autowired
    private AuthorityClient authorityClient;

    private static final Logger logger = LoggerFactory.getLogger(RunStrategyServiceImpl.class);

    @Override
    public ResultObject distributeRunStrategy(RunStrategyDistributeDTO runStrategyDistributeDTO) throws Exception {
        ResultObject wsResult = runStrategyWebsocketHandler.distributeRunStrategy(runStrategyDistributeDTO);
        int errorCode = wsResult.getErrorCode();
        if (errorCode == 9) {
            return runStrategyMqttHandler.send(runStrategyDistributeDTO);
        } else {
            return wsResult;
        }
    }

    @Override
    public ResultObject getLocalRunStrategy(RunStrategyDistributeDTO runStrategyDistributeDTO) throws Exception {
        ResultObject resultObject = runStrategyWebsocketHandler.getLocalRunStrategy(runStrategyDistributeDTO);
        if (resultObject.getErrorCode() == 9) {
            return runStrategyMqttHandler.send(runStrategyDistributeDTO);
        } else {
            return resultObject;
        }
    }

    @Override
    public ResultObject distributeHolidays(Map<String, Object> holidays) throws Exception {
        List<Station> stations = authorityClient.getStations(null).getResults();
        if (CollectionUtil.isNotEmpty(stations)) {
            for (Station station : stations) {
                if (station.getId() != null) {
                    try {
                        holidays.put("stationCode", station.getId());
                        RunStrategyDistributeDTO runStrategyDistributeDTO = new RunStrategyDistributeDTO();
                        runStrategyDistributeDTO.setStationCode(station.getId());
                        runStrategyDistributeDTO.setResults(holidays);
                        ResultObject wsResult = runStrategyWebsocketHandler.distributeRunStrategy(runStrategyDistributeDTO);
                        int errorCode = wsResult.getErrorCode();
                        if (errorCode == 9) {
                            runStrategyMqttHandler.send(runStrategyDistributeDTO);
                        }
                    } catch (Exception e) {
                        logger.error("电站{}下发节假日信息出错", station.getId(), e);
                    }
                }
            }
        }
        return new ResultObject();
    }
}
